import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainPanel extends JPanel implements
MouseListener {
	
	private Clip bgm;

	private Image title, gameStart, end = null;
	
	Rectangle rect, rect2;
	
	

	public MainPanel() {
		try{
			URL	url = getClass().getResource("opening.wav");
			AudioInputStream openbgm = 
					AudioSystem.getAudioInputStream(url);//오디오파일에 대한 스트림
			bgm = AudioSystem.getClip();
			bgm.open(openbgm);
			bgm.loop(Clip.LOOP_CONTINUOUSLY);
			}catch(Exception ex){ex.printStackTrace();}
			bgm.start();
		initUI();
	}
	
	@Override
	public void paint(Graphics g) {//g가 가상그림

		super.paint(g);
		//g = title.getGraphics();//Graphics g인자로 바로 넘어오기때문에 안해도 됨
		g.drawImage(title, 0, 0, null);
		g.drawImage(gameStart,  rect.x,  rect.y, null);
		g.drawImage(end,  rect2.x, rect2.y , null);
		
		
		
	}

	private void initUI() {
		//setLayout(null);
		setSize(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
		try {
			URL ur1 = getClass().getResource("title.png");
			Image image = ImageIO.read(ur1);
			title = image.getScaledInstance(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT,
					Image.SCALE_SMOOTH);
			
			
			ur1 = getClass().getResource("gameStart.png");
//			  image = ImageIO.read(ur1);
//			gameStart = image.getScaledInstance(Constants.SCREEN_WIDTH/4, Constants.SCREEN_HEIGHT/4,
//					Image.SCALE_SMOOTH);
			  gameStart = ImageIO.read(ur1); 
			rect =new Rectangle(gameStart.getWidth(null), gameStart.getHeight(null));

			
			ur1 = getClass().getResource("exit.png");
//			  image = ImageIO.read(ur1);
//			end = image.getScaledInstance(Constants.SCREEN_WIDTH/4, Constants.SCREEN_HEIGHT/4,
//					Image.SCALE_SMOOTH);
			end = ImageIO.read(ur1);
		
			rect2 =new Rectangle(end.getWidth(null), end.getHeight(null));

			
		
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		rect.x = (Constants.SCREEN_WIDTH-gameStart.getWidth(null))/2;//위치정보
		rect.y = (Constants.SCREEN_HEIGHT+gameStart.getHeight(null))/2;
		
		rect2.x = (Constants.SCREEN_WIDTH-end.getWidth(null))/2;//위치정보
		rect2.y = rect.y+gameStart.getHeight(null)+end.getHeight(null)/2;
		addMouseListener(this);//이미지는 리스너를 못받는다.. 컴포넌트만 리스너를 받는다.	
	}
	
		
			
		


	
	


	

	@Override
	public void mousePressed(MouseEvent e) {
		
		
	
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if ((e.getX() > rect.x &&  e.getX() <(rect.x+rect.width))
				&&(e.getY() > rect.y &&  e.getY() <(rect.y+rect.height))){
			bgm.stop();		
			validate();
			MainScreen screen = new MainScreen();
			//this.setFocusable(false);
			screen.setFocusable(true);//컴포넌트가 이벤트를 수신할 수 있도록 설정(명시적으로 너는 이벤트를 받는애다!) 받았다는 이야기는 아님
			screen.setSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
			Container frame = getParent();
			frame.add(screen);
			frame.remove(this);
			frame.validate();
			frame.repaint();
			screen.requestFocusInWindow();//컴포넌트가 입력 포커스를 취득하는것을 요청한다.  
			
		}
		else if ((e.getX() > rect2.x &&  e.getX() <(rect2.x+rect2.width))
				&&(e.getY() > rect2.y &&  e.getY() <(rect2.y+rect2.height))) {
			System.out.println("1111");
			System.exit(0);
			
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	


}
